package servlet.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.Connection;

import javabean.DateTime;
import javabean.Base;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class BookAdd
 */
@WebServlet("/user/ordercreate")
public class OrderCreate extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json; charset=utf8");
		ResultSet resultSet = null;
		String nextstep =null;
		String Oid =null;
		String uid = req.getParameter("uid");
		String rid = req.getParameter("rid");
		String location = req.getParameter("loc");
		String destination = req.getParameter("des");
		String description= req.getParameter("description");
		String status = "no-transported";
		DateTime now =new DateTime();
		String time = now.show();
		System.out.println(description+"-------------");			//debug
		JSONObject json = new JSONObject();
		Connection connection = null;
		PreparedStatement pstmt = null;
		//ResultSet resultSet = null;
		int result = 0;
		String sql = "insert into oorder(destination, detail,Uid, nowarea, state, time, Rid) values(?,?,?,?,?,?,?)";
		System.out.println(sql);
		PrintWriter out = resp.getWriter();
		try {
			connection = (Connection) Base.getConnection();
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, destination);
			pstmt.setString(2, description);
			pstmt.setString(3, uid);
			pstmt.setString(4, location);
			pstmt.setString(5, status);
			pstmt.setString(6, time);
			pstmt.setString(7, rid);
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally {
			try {
				Base.closeResource(connection, pstmt, null);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		sql="select * from oorder where Oid not in ( select  Oid from oorder NATURAL join "+location+"order ) and nowarea ='"+location+"'";
		try {
			connection = (Connection) Base.getConnection();
			pstmt = connection.prepareStatement(sql);
			resultSet = pstmt.executeQuery();
			while(resultSet.next()){
				Oid = resultSet.getString("Oid");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		sql="select * from nextstep where nowarea ='"+location+"' and destination = '"+destination+"'";
		try {
			connection = (Connection) Base.getConnection();
			pstmt = connection.prepareStatement(sql);
			resultSet = pstmt.executeQuery();
			if(resultSet.next()){
				nextstep = resultSet.getString("nextstep");
			}else{
				nextstep = location;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		sql="insert into `"+location+"order` (`Oid`,`nextstep`) values ("+Oid+",'"+nextstep+"')";
		try {
			connection = (Connection) Base.getConnection();
			pstmt = connection.prepareStatement(sql);
			result += pstmt.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(result);
		if(result==2) {
			json.put("code", "0");
			json.put("msg", "success");
		}else {
			json.put("code", "1");
			json.put("msg", "error");
		}
		out.write(json.toString());
	}

}
